<?php
/**
 * ComSpiderDao.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2020/10/27- 5:54 下午
 */

namespace common\dao;


use api\models\PddMallLogistics;
use common\exception\ValidatorException;
use common\library\BaseDao;
use common\models\com\ComSpider;

class ComSpiderDao extends BaseDao
{


    /**
     * 保存
     * @param array $data
     * @return ComSpider
     * @throws ValidatorException
     */
    public function saveData(array $data):ComSpider
    {
        $spider = new ComSpider();

        $spider->load($data,'');

        if (!$spider->validate()) {
            throw new ValidatorException($spider->errorsToString());
        }

        if($spider->save())
        {
            return $spider;
        }else{
            throw new ValidatorException($spider->errorsToOneString());
        }
    }


    /**
     * 更新
     * @param ComSpider $comSpider
     * @param array $data
     * @return ComSpider
     * @throws ValidatorException
     */
    public function updateData(ComSpider $comSpider, array $data)
    {
        $comSpider->load($data,'');

        if (!$comSpider->validate()) {
            throw new ValidatorException($comSpider->errorsToString());
        }
        if($comSpider->save())
        {
            return $comSpider;
        }else{

            throw new ValidatorException($comSpider->errorsToOneString());
        }
    }


    /**
     * 通过平台和平台唯一主键id获取信息
     * @param int $platform
     * @param string $num_iid
     * @return ComSpider|null
     */
    public function getByPlatAndNumId(int $platform,string $num_iid)
    {
        return ComSpider::findOne(['plat_id'=>$num_iid,'platform'=>$platform]);
    }


    /**
     * 通过主键获取信息
     * @param int $pk
     * @return ComSpider
     */
    public function getByPk(int $pk):ComSpider
    {
        return ComSpider::findOne(['id'=>$pk]);
    }

}